package com.siyu.controller;

import com.siyu.model.Files;
import com.siyu.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/files")
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("userId") Long userId) throws IOException, IOException {
        fileService.uploadFile(file, userId);
        return ResponseEntity.ok("File uploaded successfully");
    }

    @GetMapping("/download/{fileId}")
    public ResponseEntity<byte[]> downloadFile(@PathVariable Long fileId) throws IOException {
        byte[] file = fileService.downloadFile(fileId);
        return ResponseEntity.ok().body(file);
    }

    @GetMapping("/shared/{userId}")
    public List<Files> getSharedFiles(@PathVariable Long userId) {
        return fileService.getSharedFiles(userId);
    }
}